<?php
@set_time_limit(0);
@session_start();
@ob_start();
error_reporting (E_ALL ^ E_NOTICE);
header('Refresh: 30');
require("includes/config.inc.php"); 
require("includes/Database.class.php"); 

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
$db->connect(); 

require_once("includes/GPSFunction.php");
$temp="00-00-00 00:00:00am";
?>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<?php
function get_text($filename)
{
	$fp_load = @fopen("$filename", "rb");
	if ( $fp_load )
	{
		while ( !feof($fp_load) )
		{
			$content .= fgets($fp_load, 102400);
		}
		return $content;
	}
	else
	{
		//header("Location:dataServerConn.php");
	}
	@fclose($fp_load);
}
function getClosestElement($array, $value) 
{
	$size = count($array);
	$tmp = -1;
	if ($size > 0) 
	{
		//echo max($array)."<br><br>";
		//$ret = max($array);
		 while (list($key, $va) = each($array)) 
		 {			 
			 $diff = abs($va - $value);			
			 if($tmp == -1)
			 {
				 $tmp = $diff;
				 $k = $key;
				 //echo "1st ".$key." & ".$diff." = ".$value." diff ".$tmp."<br><br><br>";
				
			 }
			 else
			 {
				 //echo $key." & ".$diff." = ".$value." diff ".$tmp."<br>";
				 if($tmp > $diff) 
				 {
					 $tmp = $diff;
					 $k = $key;
				 }
			}
		}
		//echo "<br> ".$key." ".$va." & ".$diff." = ".$value." diff ".$tmp."<br>";
		return $k;
		//echo "<br><br>";
	}
	else 
	{
	  return NULL;
	}
}
function ReversSordData($data1)
{
	$dataArr = explode('#',$data1);
	for($i=0;$i<count($dataArr)-1;$i++)
	{
		$dtArray = explode(',',$dataArr[$i]);
		//print_r($dtArray);
		$newDate=$dtArray[9];
		$newdtArray1[$newDate] = $dataArr[$i];
	}
	ksort($newdtArray1);
 	$data = end($newdtArray1)."#";
	return $data;
}
function gpspathFun($source)
{
	$file1 = @fopen($source, "r");
	if($file1)
	{
		while(!@feof($file1))
		{
		   $data= @fgets($file1);				 
		   //$i++;
		}
		$data = getSortedData($data);
		return $data;		
	}
	else
	{
		return 0;
	}
	@fclose($file1);
}
function chk_folder($filename)
{
	$fp_load = @fopen("$filename", "rb");
	if ( $fp_load )
	{
		return true;
	}
	else
	{
		return false;
	}
}
function getFuel($data,$strtTime,$endliveime,$fuel_port,$fuel_params,$ent_val_mnt)
{
	//echo $strtTime.','.$endliveime.','.$fuel_port.','.$fuel_params.','.$ent_val_mnt;exit;
	$timeArr= array();
	$strTime = '';
	$endTime = '';
	$count = 1;
	$tmpVal = -1;
	$cnt = 0;
	$totalDistance = 0;
	$flArr1 = array();
	$flArr2 = array();
	$tmpFl = 0;
	if(count($data)>0)
	{
		 $data1=explode("#",$data);
		 $j1 = 0;
	 //echo count($data1); 
	  while($j1 < count($data1))
	 {
		$data2=explode("@",$data1[$j1]);
		//echo '<pre>'; print_r($data2);echo '</pre>';
		if(count($data2)>1)
		{
			$data3=explode(",",$data2[1]);
			$vehi=$data3[0];
			$geodate = $data3[8]." ".$data3[9];
			$geoTime = date("H:i:s",strtotime($data3[9]));
			$curTime = explode(":",date("H:i:s",strtotime($data3[9])));
			$newTime  = (($curTime[0]) * 60) + $curTime[1];
			$curTime = (($curTime[0]) * 3600) + ($curTime[1]*60);
			 
			
			if($newTime  >= $strtTime && $newTime <=$endliveime)
			{
			if(!in_array($geoTime,$timeArr))
			{
				$pos1=calLat($data3[2]);
				$pos2=calLong($data3[1]);
				
				if($pos1>0 && $pos2>0)
				{					
					if(!in_array($geoTime,$timeArr))
					{
						if($cnt==0)
						{
							$pits1 =  $pos1;
							$pits2 =  $pos2;
						}
						else
						{
							$pits3 =  $pos1;
							$pits4 =  $pos2;
							//echo $sTime1." >= ".$curTime." <= ".$eTime1." ".$totalDistance."<br>";
							$dist = getDistance($pits1, $pits2, $pits3, $pits4);
							if($dist <= 10)
							{
								$totalDistance += $dist;
							}
							$pits1 =  $pits3;
							$pits2 =  $pits4;
						}
						$v = splitParameter($fuel_port,$data3[7]);
						if(strlen($v) > 3)
						{
							$v = $v/pow(10,(strlen($v)-1));
							$flVal = getClosestElement($fuel_params,$v);
							//echo "<br>";
						}
						elseif(strlen($v) == 3)
						{
							$v = $v/pow(10,(strlen($v)));
							$flVal = getClosestElement($fuel_params,$v);
							//echo "<br>";
						}
						else $flVal = -1;
						//if($flVal != -1)
						//echo $flVal.' '.date("h:i:s",strtotime($geoTime)).",".round($totalDistance)."<br>";
						//echo $flVal.' '.$cnt.'<br>';
						if($flVal != -1)
						{
						//	echo " ".$flVal.",".$data3[8].",".$geoTime.",".$pos1.",".$pos2."<br>";
						}
						if($flVal != -1 && $cnt == 0)
						{
							$strtFVal = $flVal;
							$strPoint = getMyGeoAddress($pos1,$pos2);
							$flArr1[$flVal] = $flVal.",".$data3[8].",".$geoTime.",".$pos1.",".$pos2.",".$strPoint.",".round($totalDistance);	//.','.$flVal.','.$geoTime;
							$cnt++;
						}
						elseif($flval != -1 && $cnt > 0)
						{
							$tmpFl = ($strtFVal -1);
							if (!array_key_exists($tmpFl, $flArr1) && $tmpFl == $flVal) 
							{					
								$strtFVal = $flVal;			
								$strPoint = getMyGeoAddress($pos1,$pos2);
								$flArr1[$flVal] = $flVal.",".$data3[8].",".$geoTime.",".$pos1.",".$pos2.",".$strPoint.",".round($totalDistance);	//.','.$flVal.','.$geoTime;
							}
							/*else
							{
								//$strtFVal = $flVal;			
								$flArr2[$flVal] = round($totalDistance).','.$flVal.','.$geoTime;
							}*/
							$cnt++;
						}
					}
				}
			}
		}
	}
		
	$j1++;
	}
	//echo $flVal.' '.$geoTime;
	/*if($strTime != "" && $endTime == "")
	{
		$endTime=$geoTime;
		
		$tmpVal = round($tmpVal / $count);
		$altData [] = $tmpVal.",".$data3[8].",".$strTime.",".$endTime.",".gmdate("H:i:s",(strtotime($endTime) - strtotime($strTime)));
	}*/
	}
	$altData = $flArr1;
	//print_r($altData);
	//print_r($flArr2);
	//echo implode("#",$altData)."<br>";
	if(count($altData) > 0)
		return implode("#",$altData);
	else 
		return "";
}
function getFuelPercent($data,$strtTime,$endliveime,$fuel_port,$fLabel,$fValue,$fuel_params)
{
	//echo $strtTime.','.$endliveime.','.$fuel_port.','.$fuel_params.','.$ent_val_mnt;exit;
	$timeArr= array();
	$strTime = '';
	$endTime = '';
	$tmpTime = '';
	$count = 1;
	$tmpVal = -1;
	$cnt = 0;
	$totalDistance = 0;
	$flArr1 = array();
	$flArr2 = array();
	$tmpFl = 0;
	$strtFVal = '';
	
	if(count($data)>0)
	{
		 $data1=explode("#",$data);
		 $j1 = 0;
	 //echo count($data1); 
	  while($j1 < count($data1))
	 {
		$data2=explode("@",$data1[$j1]);
		//echo '<pre>'; print_r($data2);echo '</pre>';
		if(count($data2)>1)
		{
			$data3=explode(",",$data2[1]);
			$vehi=$data3[0];
			$geodate = $data3[8]." ".$data3[9];
			$geoTime = date("H:i:s",strtotime($data3[9]));
			$curTime = explode(":",date("H:i:s",strtotime($data3[9])));
			$newTime  = (($curTime[0]) * 60) + $curTime[1];
			$curTime = (($curTime[0]) * 3600) + ($curTime[1]*60);
			 
			
	if($newTime  >= $strtTime && $newTime <=$endliveime)
	{
		if(!in_array($geoTime,$timeArr))
		{
			$v1 = splitParameter($fuel_port,$data3[7]);
			if(strlen($v1) > 3)			//$fLavel,$fValue
			{
				$v = $v1/pow(10,(strlen($v1)-1));
			}
			elseif(strlen($v1) == 3)
			{
				//$v = $v1/pow(10,(strlen($v1)));
				//$flVal = getClosestElement($fuel_params,$v);
				//echo "<br>";
			}
			else $v = 0;				
			
			if($v > 0)
			{
			//$v = round($v,2);
			//echo $v.' d1 '.$fLabel[0].'='.$fValue[0].' d2 '.$fLabel[1].'='.$fValue[1].' d3 '.$fLabel[2].'='.$fValue[2];
			//echo $v.' ' .$geoTime;
			//echo "<br>";			
			//asort($fuel_params);
			
			//print_r($fuel_params);
			//print_r($firstHalf);
			$flVal = getClosestElement($fuel_params,$v);
			//print_r($fLabel);
			if($flVal != '')
			{
			$pos1=calLat($data3[2]);
			$pos2=calLong($data3[1]);
			if($data3[3] == 0)
			{
				$strtFVal = $data3[8].",".$pos1.",".$pos2.",".$flVal.",".$geoTime;	//.','.$flVal.','.$geoTime;
				if(!array_key_exists($flVal,$flArr2))
				{
					$flArr2[$flVal] = $geoTime;
				}
				else
				{
					$flArr2[$flVal] .= '#'.$geoTime;
				}
				//$tStmp = $flVal.','.$geoTime;
				
				//array_push($flArr2,$tStmp);
				//$flArr2[$flVal] = 
				//$strPoint = "<a href=http://maps.google.com/maps?q=".$pos1.",".$pos2." target=_blank title=click here>".$pos1.",".$pos2."</a>";
				//$altData[$flVal] = $flVal.",".$data3[8].",".$geoTime.",".$pos1.",".$pos2.",".$strPoint;	//.','.$flVal.','.$geoTime;
				$cnt++;
			}
			elseif($data3[3] > 0 && $strtFVal!='')
			{
				$tcnt = 0;
				$avgVal = '';
				if(count($flArr2) > 0 )
				{
					foreach ($flArr2 as $key => $value) 
					{
						$cVal = explode("#",$value);
						if(count($cVal) > $tcnt)
						{
							$avgVal = $key."_".$value;
							$tcnt = count($cVal);
						}
					}
					if($avgVal !='')
					{
						$sp1 = explode("_",$avgVal);
						$sp2 = explode("#",$sp1[1]);
						$avgVal = $sp1[0].','.$sp2[count($sp2)-1];
					}
				}
				else
				{
					$avgVal = '';
				}
				//print_r($flArr2);
				$altData[$cnt]  = $strtFVal.",".$flVal.','.$geoTime.','.$avgVal;
				$strtFVal= '';
				$flArr2 = array();
				$tcnt = 0;
				$avgVal = '';
				$cnt++;
			}
			}
			
			}
			}
		}
	}
	$j1++;
	}
}
	if($strtFVal != '')
	{
		//$altData[$cnt]  = $strtFVal;
		$altData[$cnt]  = $strtFVal.",".$flVal.','.$geoTime.','.$avgVal;
	}
	//print_r($altData);
	//exit;
	//echo $altData;
	//echo implode("#",$altData)."<br>";
	if(count($altData) > 0)
		return implode("#",$altData);
	else 
		return "";
}

?>
<form name="frmName" id="frmName" action="" method="get">
	<label>Enter Imei number of a vehicle</label>
	<input type="text" name="txtImei" id="txtImei" value="352848025711462" />(Fuel reading should be proper)
	<input type="submit" name="btnSubmit" id="btnSubmit" value="Click" />
</form>
<?php
if($_GET[txtImei] != '')
{
$date_offline = date("d-m-Y");
$txtVehicleImei = $_GET[txtImei];		//"352848026043626";
$devices_query = "SELECT * FROM tb_deviceinfo,tb_client_subscription WHERE tcs_isActive = 1 AND tcs_deviceId = di_id AND di_status = 1 AND di_imeiId=".$txtVehicleImei."  ORDER BY di_deviceName,di_deviceId ASC";
$devices_resp = mysql_query($devices_query);	

$devices_fetch = @mysql_fetch_assoc($devices_resp);
//print_r($devices_fetch);
$analogData = explode("=",$devices_fetch[di_analogPort]);
$rdAlertBy  = $analogData[1];
//exit;
$d1[1] = $rdAlertBy;
//	echo strpos($d1[1],"("); exit;
$d2 = substr($d1[1],0,strpos($d1[1],"("));
$n = strpos($d1[1],"(")+1;
$d3 = str_replace(")","",substr($d1[1],$n));
$d3 = explode("#",$d3);
$port = $d2;
/*
for($l = 0; $l < count($d3)-1; $l++)
{
	$d4 = explode("_",$d3[$l]);
	$params[$d4[0]] = $d4[1];
}*/
for($l = 0; $l < count($d3)-1; $l++)
{
	$d4 = explode("_",$d3[$l]);
	$fuel_params[$d4[0]] = $d4[1];
	$fLabel[] = $d4[0];
	$fValue[] = $d4[1];
	//vol[l]=d4[1];
	//ltr[l]=d4[0];
}
//print_r($fuel_params);
//exit;
sort($fLabel);
sort($fValue);
$v = 0;
//print_r($fLabel);
while($v < count($fValue)-1)
{
	$l = $v+1;
	$firstVal = $fValue[$v];
	$secondVal = $fValue[$l];

	$firstLab = $fLabel[$v];
	$secondLab = $fLabel[$l];
	
	//echo $firstVal.' '.$secondVal.' '.$firstLab.' '.$secondLab;
	$labDif1 = ($secondVal - $firstVal) / ($secondLab-$firstLab);
	//echo "<br>";
	$strVal = $firstVal;
	for($k = $firstLab; $k<=$secondLab; $k++)
	{
		//echo "<br>";
		$firstHalf[$k] = $strVal;
		//echo $k.' = '.$strVal;
		$strVal = $strVal + $labDif1;							
		//echo "<br>";
	}
	//exit;
	//echo "<br>";
	$v++;
}
echo "<br><br>";
$path1 = DATAPATH.date('d-m-Y',strtotime($date_offline))."/".$txtVehicleImei.".txt";
//gpspathFun($path1);
$devdata =  gpspathFun($path1);		
//$res = getFuel($devdata ,0,1439,$port,$params,1);
$finData = getFuelPercent($devdata,0,1439,$port,$fLabel,$fValue,$firstHalf);
	if(count($finData) > 0)
	{
?>
<table cellpadding="0" cellspacing="0" border="0" class="gridform_final">      
<thead>
	<tr>
    	<th width="5%">#</th>
	  	<th width="15%">Date (dd-mm-yy) </th>
        <th>Device IMEI</th>
        <th width="12%">Fuel A - Time</th>
		<th width="12%">Fuel B - Time</th>
        <th width="12%">Fuel C - Time</th>   		
     </tr>
  </thead>
  <tbody>
      
<?php
	if(count($finData) > 0)
	{
		$tTdiff1 = explode("#",$finData);
		//print_r($tTdiff1); exit;
		for($k=0;$k<count($tTdiff1);$k++)
		{
		$tTdiff = explode(",",$tTdiff1[$k]);
		//print_r($tTdiff);
		//$strPoint = "<a href=http://maps.google.com/maps?q=".$tTdiff[3].",".$tTdiff[4]." target=_blank title=click here>".$tTdiff[3].",".$tTdiff[4]."</a>";
		?>		
		 <tr <?php if($r % 2 == 0) { echo 'class="odd_row"';} else { echo 'class="even_row"'; } ?> >
		   <td valign="top"><?php echo $k+1; ?></td>
   		   <td valign="top"><?php echo $tTdiff[0]; ?></td>
		   <td valign="top"><?php echo $_GET[txtImei]; ?></td>
		   <td valign="top"><?php echo $tTdiff[3].' - '.date('h:i:s A',strtotime($tTdiff[4])); ?></td>
		   <td valign="top"><?php echo $tTdiff[5].' - '.date('h:i:s A',strtotime($tTdiff[6])); ?></td>
		   <td valign="top"><?php echo $tTdiff[7].' - '.date('h:i:s A',strtotime($tTdiff[8])); ?></td>
		 </tr>
		<?php 
		//exit;
		}
	}
?>
</tbody>
</table>
<?php
}
}
?>
